<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录界面</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="./js/ajax.js"></script>
    <script src="./js/crypto-js.js"></script>
</head>
<body>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">用户登录</h3>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label for="">用户编码</label>
                    <input type="text" class="form-control" id="id" placeholder="请输入用户编码">
                </div>
                <div class="form-group">
                    <label for="">用户密码</label>
                    <input type="password" class="form-control" id="password" placeholder="请输入用户密码">
                </div>
                <button type="submit" class="btn btn-primary" onclick="loginEvt()">登录</button>
                <button type="submit" class="btn btn-primary" onclick="toRegister()">注册</button>
            </div>
        </div>
    </div>
    <script>
        function toRegister() {
            window.location.href = './register.html'
        }

        function loginEvt() {
            ajax({
                method: 'POST',
                url: '/user/login',
                data: {
                    id: document.getElementById('id').value,
                    password: CryptoJS.MD5(document.getElementById('password').value).toString()
                }
            }).then(result => {
                sessionStorage.setItem('TOKEN', result.token)
                if (result.code === 200) {
                    window.location.href = './userList.html'
                } else {
                    alert(result.message)
                }
            }).catch(err => {
                alert('服务器错误！')
            })
        }
    </script>
</body>
</html>